//获取登录状态
isLogin();

//将本地存储欲购买的书信息添加至购物车
var buying_books_arr = JSON.parse(localStorage.getItem('buying_bookMsg'));

function addBuyingBooks() {
    var cart_tbody = $$('cart_tbody')[0];

    for (var i = 0 ; i < buying_books_arr.length ; i++) {
        var newDiv = document.createElement('div');
        newDiv.className = 'books_list';
        newDiv.innerHTML = '<span><input type="checkbox" onclick="selectBook(this)"></span> <span class="cart_book_img"><img src="' + buying_books_arr[i].buying_book_img + '" alt=""></span> <span class="cart_book_name">' + buying_books_arr[i].buying_book_name + '</span><span class="cart_book_price">¥' + buying_books_arr[i].buying_book_price + '</span><span><button class="amount_sub" onclick="subAmount(this)">-</button><button class="amount">' + buying_books_arr[i].buying_book_num + '</button><button class="amount_add" onclick="addAmount(this)">+</button></span><span class="get_total_money"><strong></strong></span><span onclick="deleteBookRow(this)">删除</span>';
        cart_tbody.appendChild(newDiv);
        var get_total_money = $$('get_total_money')[i];
        get_total_money.children[0].innerHTML = '¥' + buying_books_arr[i].buying_book_num * buying_books_arr[i].buying_book_price;
    }

}

addBuyingBooks();

//购物车中减少数量
function subAmount(btn) {
    var thisAmount_btn = btn.nextSibling;
    if (thisAmount_btn.innerHTML !== '1') {
        btn.style.disabled = false;
        thisAmount_btn.innerHTML--;
    } else if (thisAmount_btn.innerHTML === '1') {
        btn.style.disabled = true;
    }

    for (var i = 0 ; i < buying_books_arr.length ; i++) {
        //判断此行为那本书
        if (btn.parentNode.parentNode.children[2].innerHTML === buying_books_arr[i].buying_book_name) {
            buying_books_arr[i].buying_book_num = thisAmount_btn.innerHTML;
            btn.parentNode.parentNode.children[5].children[0].innerHTML = '¥' + buying_books_arr[i].buying_book_price * buying_books_arr[i].buying_book_num;
        }
    }
    calTotalMoney();
    localStorage.setItem('buying_bookMsg' , JSON.stringify(buying_books_arr));

}

//购物车中添加数量
function addAmount(btn) {
    var thisAmount_btn = btn.previousSibling;
    thisAmount_btn.innerHTML++;

    for (var i = 0 ; i < buying_books_arr.length ; i++) {
        //判断此行为那本书
        if (btn.parentNode.parentNode.children[2].innerHTML === buying_books_arr[i].buying_book_name) {
            buying_books_arr[i].buying_book_num = thisAmount_btn.innerHTML;
            btn.parentNode.parentNode.children[5].children[0].innerHTML = '¥' + buying_books_arr[i].buying_book_price * buying_books_arr[i].buying_book_num;
        }
    }
    calTotalMoney();
    localStorage.setItem('buying_bookMsg' , JSON.stringify(buying_books_arr));

}

//删除
function deleteBookRow(btn) {
    for (var i = 0 ; i < buying_books_arr.length ; i++) {
        //判断此行为那本书
        if (btn.parentNode.children[2].innerHTML === buying_books_arr[i].buying_book_name) {
            buying_books_arr.splice(i , 1);
            btn.parentNode.style.display = 'none';
        }
    }
    select_amount.innerHTML='0';
    total_money.innerHTML='0'
    localStorage.setItem('buying_bookMsg' , JSON.stringify(buying_books_arr));
}

//全选
var select_amount = $('select_amount');
var bookLists = $$('books_list');
var total_money = $('total_money');

function selectAll(btn) {
    for (var i = 0 ; i < bookLists.length ; i++) {
        bookLists[i].children[0].children[0].checked = !!btn.checked;
    }
    //获取选择商品件数
    if (btn.checked) {
        arrGetMoney();
        if (buying_books_arr.length === 0) {
            select_amount.innerHTML = 0;
        } else {
            select_amount.innerHTML = bookLists.length;
        }
    } else {
        calTotalMoney();
        select_amount.innerHTML = 0;
    }
}

//通过arr拿到总价
function arrGetMoney() {
    var tMoney = 0;
    for (var j = 0 ; j < buying_books_arr.length ; j++) {
        tMoney = Number(tMoney) + Number(buying_books_arr[j].buying_book_num) * Number(buying_books_arr[j].buying_book_price);
    }
    total_money.innerHTML = tMoney;
}

//每行选择
function selectBook(btn) {
    var select_all = $('selectAll');
    var bookLists = btn.parentNode.parentNode.parentNode.children;
    var flag = true;

    //设置全选状态
    for (var i = 0 ; i < bookLists.length ; i++) {
        if (!bookLists[i].children[0].children[0].checked) {
            flag = false;
            break;
        }
    }
    select_all.checked = flag;
    //获取选择商品件数
    if (btn.checked) {
        select_amount.innerHTML++;
    } else {
        select_amount.innerHTML--;
    }

    calTotalMoney();
}


//计算总价
var cart_tbody = $$('cart_tbody')[0];

function calTotalMoney() {
    var money = 0;
    for (var i = 0 ; i < cart_tbody.children.length ; i++) {
        if (cart_tbody.children[i].children[0].children[0].checked) {
            money = money + Number(cart_tbody.children[i].children[5].children[0].innerHTML.replace(/[^0-9]/ig , ""));
        }
    }
    total_money.innerHTML = money;
}


//删除选中商品
function deleteSelect() {
    for (var i = cart_tbody.children.length - 1 ; i >= 0 ; i--) {
        if (cart_tbody.children[i].children[0].children[0].checked) {
            buying_books_arr.splice(i , 1);
            cart_tbody.children[i].style.display = 'none';
            total_money.innerHTML = 0;
            select_amount.innerHTML = 0;
        }
    }
    localStorage.setItem('buying_bookMsg' , JSON.stringify(buying_books_arr));
}

//结算
function payMoney() {
    for (var i = cart_tbody.children.length - 1 ; i >= 0 ; i--) {
        if (cart_tbody.children[i].children[0].children[0].checked) {
            var money=total_money.innerHTML;
            buying_books_arr.splice(i , 1);
            cart_tbody.children[i].style.display = 'none';
        }
    }
    alert("购买成功，付款"+money+"元");
    total_money.innerHTML=0;
    select_amount.innerHTML=0;

    localStorage.setItem('buying_bookMsg' , JSON.stringify(buying_books_arr));
}
